﻿using Microsoft.AspNetCore.SignalR.Client;

using NLog.Fluent;
using Simple.BasicNet.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 App.Extension
 * 接口名称 RandomRetryPolicy
 * 开发人员：11920
 * 创建时间：2023/4/17 11:17:11
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace App.Extension
{
	public class RandomRetryPolicy : IRetryPolicy
	{
		public RandomRetryPolicy(int count, string url, string deviceSn,ILogger logger)
		{
			Count = count;
			Url = url;
			DeviceSn = deviceSn;
			this.logger = logger;
		}

		private readonly Random _random = new Random();

		public int Count { get; }
		public string Url { get; }
		public string DeviceSn { get; }
		public ILogger logger { get; }

		public TimeSpan? NextRetryDelay(RetryContext retryContext)
		{
			// If we've been reconnecting for less than 60 seconds so far,
			// wait between 0 and 10 seconds before the next reconnect attempt.

			if (retryContext.PreviousRetryCount < Count)
			{
				logger.Info($"设备[{DeviceSn}]正在重连{Count - retryContext.PreviousRetryCount}");
				return TimeSpan.FromSeconds(_random.NextDouble() * 10);
			}
			else
			{
				logger.Info($"服务器连接失败>>>{Url}>>>设备[{DeviceSn}]");
				// If we've been reconnecting for more than 60 seconds so far, stop reconnecting.
				return null;
			}
		}
	}
}
